﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using BrainTechLLC.WPF;
using BrainTechLLC;
using System.ComponentModel;

namespace DirectoryViewModels
{
    public class DirectoriesToExamineViewModel : ObservableCollection<DirectoryToExamineViewModel>, INotifyPropertyChanged
    {
        public new event PropertyChangedEventHandler PropertyChanged;

        protected DirectoriesToExamine _dirsToExamine;
        protected bool _IsSelected;
        protected bool _IsInEditMode;
        public SimpleDispatcherTimer _addMultipleFoldersTimer;
        public int _addedCount;

        protected override void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChanged.RaiseEvent(this, e.PropertyName);
            base.OnPropertyChanged(e);
        }

        public bool IsSelected
        {
            get { return _IsSelected; }
            set
            {
                if (_IsSelected != value)
                {
                    _IsSelected = value;
                    OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("IsSelected"));
                }
            }
        }

        public bool IsInEditMode
        {
            get { return _IsInEditMode; }
            set
            {
                if (_IsInEditMode != value)
                {
                    _IsInEditMode = value;
                    OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("IsInEditMode"));
                }
            }
        }

        public string Name
        {
            get { return _dirsToExamine.Name; }
            set
            {
                if (_dirsToExamine.Name != value)
                {
                    _dirsToExamine.Name = value;
                    OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("Name"));
                }
            }
        }

        public void Refresh()
        {
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        public DirectoriesToExamineViewModel() { _dirsToExamine = new DirectoriesToExamine(); }
        public DirectoriesToExamineViewModel(DirectoriesToExamine dirs)
        {
            _dirsToExamine = dirs;

            //var v = from dir in dirs.AsParallel()
            //        select dir;

            //v.ForAll(dir =>
            //{
            //    var vm = new DirectoryToExamineViewModel(dir);
            //    Add(vm);
            //});
            foreach (var dir in dirs)
            {
                var vm = new DirectoryToExamineViewModel(dir);
                Add(vm);
            }
        }

        public void TrackBeginAddDirectory()
        {
            _addedCount++;

            if (_addMultipleFoldersTimer == null)
            {
                _addMultipleFoldersTimer = new SimpleDispatcherTimer(1200, (timer) =>
                {
                    if (_addedCount == 0)
                    {
                        OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("EndingSelection"));
                        _addMultipleFoldersTimer = null;
                        return RunInitializationResult.StopTimer;
                    }
                    return RunInitializationResult.DidntDoAnything;
                });
                OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("StartingSelection"));
                _addMultipleFoldersTimer.Start();
            }
        }

        public void RemoveDirs(List<string> dirs)
        {
            for (int n = 0; n < dirs.Count; n++)
            {
                RemoveDir(dirs[n]);
            }
        }

        public void RemoveDir(string s)
        {
            var dir = _dirsToExamine.FirstOrDefault((d) => d.Directory.Equals(s, StringComparison.OrdinalIgnoreCase));
            if (dir != null)
            {
                _dirsToExamine.Remove(dir);
            }

            var dirVM = this.FirstOrDefault((vm) => vm.Directory.Equals(s, StringComparison.OrdinalIgnoreCase));

            if (dirVM != null)
            {
                Remove(dirVM);
            }
        }

        public void AddDir(string s)
        {
            DirectoryToExamine dir = new DirectoryToExamine(s);
            var vm = new DirectoryToExamineViewModel(dir);
            _dirsToExamine.Add(dir);
            Add(vm);
        }

        public void Save(string pathToExaminePath)
        {
            _dirsToExamine.Save(pathToExaminePath);
        }

        public void DeleteThumbnailCache()
        {
            for (int n = 0; n < _dirsToExamine.Count; n++)
            {
                var dir = _dirsToExamine[n];
                DirectoryInfo d = new DirectoryInfo(dir.Directory);
                FileSystemInfo[] infos = d.GetFileSystemInfos();
                for (int i = 0; i < infos.Length; i++)
                {
                    var info = infos[i];
                    if ((info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                    {
                        if (Path.GetExtension(info.Name).Equals(".thumb", StringComparison.OrdinalIgnoreCase))
                        {
                            File.Delete(info.FullName);
                        }
                    }
                }
            }
        }
    }
}
